Viewer Loading Manager
The ViewerLoadingManager class in the @promaton/scan-viewer package is a utility designed to manage loading requests efficiently. It achieves this by caching responses and de-duplicating parallel requests. While primarily intended for internal use or through ViewerObjectUtils, it can also be leveraged for custom loaders and advanced use cases.
Constructor
new ViewerLoadingManager()
Creates a new instance of the ViewerLoadingManager.
Returns:
ViewerLoadingManager
Properties
cachedLoaders
A private static property that stores cached loaders. It is a Map where the keys are loader functions, and the values are the corresponding ViewerLoader instances.
Methods
clearCache(key?)
Clears all or specific cached loading outputs.
Parameters:
key? _(unknown[])_: Optional. Specifies the cache key to clear.
Returns:
void
disposeLoaders()
Disposes and clears all cached loaders. While loaders are reused for multiple files, they may keep resources like workers alive if not disposed.
Returns:
void
getCacheKey<T>(Proto, input)
Generates a cache key for a specified loader and input.
Type Parameters:
T: The type of the loader.
Parameters:
Proto_(() => ViewerLoader<T>)_: The loader function.input_(string | string[])_: The input for the loader.
Returns:
(string | () => ViewerLoader<T>)[]
load<T>(Proto, input, onProgress?, options?)
A private static method that loads data using the specified loader, with support for caching and progress tracking.
Type Parameters:
T: The type of the loaded data.
Parameters:
Proto _(() => ViewerLoader<T>)_: The loader function.input _(string | string[])_: The input for the loader.onProgress? _((e, url?) => void)_: Optional. A callback for progress updates.options? _(LoadOptions)_: Optional. Additional loading options.
Returns:
Promise<T>
loadViewerObject<T>(Proto, input, onProgress?, options?)
Loads an object using the specified loader, either from cache or by pre-loading it.
Type Parameters:
T: The type of the loaded object.
Parameters:
-
Proto(() => ViewerLoader<T>): The loader function. -
input(string | string[]): The input for the loader. -
onProgress?((e, url?) => void): Optional. A callback for progress updates. -
options?(LoadOptions): Optional. Additional loading options.
Returns:
Promise<T>
useViewerLoader<T>(Proto, input, onProgress?, options?)
A hook to load objects using the specified loader, with caching and de-duplication.
Type Parameters:
T: The type of the loaded object.
Parameters:
-
Proto(() => ViewerLoader<T>): The loader function. -
input(string | string[]): The input for the loader. -
onProgress?((e, url?) => void): Optional. A callback for progress updates. -
options?(LoadOptions): Optional. Additional loading options.
Returns:
T
Summary
The ViewerLoadingManager is a powerful utility for managing loading requests in the @promaton/scan-viewer package. By leveraging its caching and de-duplication capabilities, developers can optimize resource usage and improve performance in their applications. Whether used internally or for advanced custom loaders, this class provides a robust solution for handling complex loading scenarios.